package lacus.wd_cinema.controller.api;

import jakarta.servlet.http.HttpServletRequest;
import lacus.wd_cinema.dto.users.UserCreateRequest;
import lacus.wd_cinema.dto.users.UserCreateResponse;
import lacus.wd_cinema.dto.users.UserUpdateRequest;
import lacus.wd_cinema.entity.User;
import lacus.wd_cinema.service.AuthService;
import lacus.wd_cinema.service.UserService;
import org.springframework.web.bind.annotation.*;

/**
 * 用户接口控制类
 * @author lacus
 * @version 0.6
 * @since 0.4
 */
@RestController
@RequestMapping("/api/users")
public class UserController {
    private final UserService userService;
    private final AuthService authService;

    public UserController(UserService userService, AuthService authService) {
        this.userService = userService;
        this.authService = authService;
    }

    @PostMapping
    public UserCreateResponse createUser(
            @RequestBody UserCreateRequest request,
            HttpServletRequest servletRequest) {
        User operator = authService.getCurrentUser(servletRequest);
        return userService.createUser(request, operator);
    }

    @PostMapping("/{id}")
    public void updateUser(
            @PathVariable long id,
            @RequestBody UserUpdateRequest request,
            HttpServletRequest servletRequest) {
        User operator = authService.getCurrentUser(servletRequest);
        userService.updateUser(id, request, operator);
    }

    @PostMapping("/{id}/password/reset")
    public void resetPassword(
            @PathVariable long id,
            HttpServletRequest servletRequest) {
        User operator = authService.getCurrentUser(servletRequest);
        userService.resetPassword(id, operator);
    }

    @DeleteMapping("/{id}")
    public void deleteUser(
            @PathVariable long id,
            HttpServletRequest servletRequest) {
        User operator = authService.getCurrentUser(servletRequest);
        userService.deleteUser(id, operator);
    }

    @PostMapping("/me/password")
    public void changeMyPassword(
            @RequestParam(name = "value") String newPassword,
            HttpServletRequest servletRequest) {
        User user = authService.getCurrentUser(servletRequest);
        userService.changePassword(user, newPassword);
    }

    @PostMapping("/me/phoneNumber")
    public void changeMyPhoneNumber(
            @RequestParam(name = "value") String newPhoneNumber,
            HttpServletRequest servletRequest) {
        User user = authService.getCurrentUser(servletRequest);
        userService.changePhoneNumber(user, newPhoneNumber);
    }
}
